package com.sitech.ibnms.core;

import com.opensymphony.xwork2.ActionContext;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;

public class JGridBaseAction extends BaseIbnmsAction {

    private static final long serialVersionUID = 0x1d59498e1c56e602L;
    /**
     * 当前页数
     */
    private int page;
    /**
     * 每页显示行数
     */
    private int pageSize;
    /**
     * 排序字段
     */
    private String sidx;
    /**
     * 排序方式
     */
    private String sord;
    /**
     * 当表格模式为TreeGrid时，代表点击的节点Id
     */
    private String nodeid;
    /**
     * 当表格模式为TreeGrid时，代表点击的节点的层级
     */
    private int n_level = -1;
    /**
     * 查询结果
     */
    private IPage result;
    private boolean search;
    protected Map searchFields;
    protected Map searchArrays;
    private PageForm pageForm;

    public JGridBaseAction() {
        searchFields = new HashMap(0);
        searchArrays = new HashMap(0);
    }

    private void populateJqGridData() {
        Map params = ActionContext.getContext().getParameters();
        Set keys = params.keySet();
        Iterator keysIt = keys.iterator();
        do {
            if (!keysIt.hasNext()) {
                break;
            }
            String key = (String) keysIt.next();
            if (!(params.get(key) instanceof String[])) {
                break;
            }
            String values[] = (String[]) params.get(key);
            if ("sidx".equals(key)) {
                if (null != values && values.length > 0) {
                    sidx = values[0];
                }
            } else if ("sord".equals(key)) {
                if (null != values && values.length > 0) {
                    sord = values[0];
                }
            } else if ("_search".equals(key)) {
                if (null != values && values.length > 0 && StringUtils.isNotBlank(values[0])) {
                    search = Boolean.parseBoolean(values[0]);
                }
            } else if ("rows".equals(key)) {
                if (null != values && values.length > 0 && StringUtils.isNotBlank(values[0])) {
                    pageSize = Integer.parseInt(values[0]);
                }
            } else if ("page".equals(key)) {
                if (null != values && values.length > 0 && StringUtils.isNotBlank(values[0])) {
                    page = Integer.parseInt(values[0]) - 1;
                }
            } else if ("nodeid".equals(key)) {
                if (null != values && values.length > 0) {
                    nodeid = values[0];
                }
            } else if ("n_level".equals(key)) {
                if (null != values && values.length > 0 && StringUtils.isNotBlank(values[0])) {
                    n_level = Integer.parseInt(values[0]);
                }
            } else if (key.endsWith("Array")) {
                searchArrays.put(key, values);
            } else if (null != values && values.length > 0) {
                searchFields.put(key, values[0]);
            }
        } while (true);
    }

    public PageForm getPageForm() {
        populateJqGridData();
        pageForm = new PageForm(pageSize, page, sidx, sord);
        pageForm.setLevel(n_level + 1);
        pageForm.setParentId(nodeid);
        if (StringUtils.isNotBlank(nodeid)) {//如果查询子节点，就不分页
            pageForm.setPageSize(Integer.MAX_VALUE);
        }
        return pageForm;
    }

    public void setPageForm(PageForm pageForm) {
        this.pageForm = pageForm;
    }

    public Map getSearchArrays() {
        return searchArrays;
    }

    public void setSearchArrays(Map searchArrays) {
        this.searchArrays = searchArrays;
    }

    public boolean isSearch() {
        return search;
    }

    public void setSearch(boolean search) {
        this.search = search;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public String getSidx() {
        return sidx;
    }

    public void setSidx(String sidx) {
        this.sidx = sidx;
    }

    public String getSord() {
        return sord;
    }

    public void setSord(String sord) {
        this.sord = sord;
    }

    public IPage getResult() {
        return result;
    }

    public void setResult(IPage result) {
        if (result.getTotal() <= result.getPage()) {
            result.setPage(result.getTotal());
        } else {
            result.setPage(page + 1);
        }
        this.result = result;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public Map getSearchFields() {
        return searchFields;
    }

    public void setSearchFields(Map searchFields) {
        this.searchFields = searchFields;
    }

    public void setJSON(Object obj) {
        JSONObject jsonObject = JSONObject.fromObject(obj);

        try {
            HttpServletResponse response = getResponse();
            response.setCharacterEncoding("UTF-8");
            PrintWriter pw = response.getWriter();
            pw.print(jsonObject);
            pw.flush();
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
